package com.cq.health.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.cq.health.dao.CheckGroupDao;
import com.cq.health.entity.PageResult;
import com.cq.health.entity.QueryPageBean;
import com.cq.health.pojo.CheckGroup;
import com.cq.health.service.CheckGroupService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/5/12 15:56
 * @Description: 检查组业务实现类
 * @Version: 1.0
 */
@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        log.debug(">>>>>>>>checkGroup:{} checkItemIds:{}"+checkGroup,checkItemIds);
        //先添加检查组，获取检查id
        checkGroupDao.add(checkGroup);
        log.debug(">>>>>>>>checkGroup_id:{}"+checkGroup.getId());
        //map（checkgroup_id，检查组id）
        //map（checkitem_id，检查项id）
        //map（检查组id，检查项id）
        //遍历选中的检查项列表，逐一添加到关系表
        for (Integer checkItemId : checkItemIds) {
            HashMap map = new HashMap();
            map.put("checkgroup_id",checkGroup.getId());
            map.put("checkitem_id",checkItemId);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }

    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug(">>>>>>>>queryPageBean:{}"+queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> checkGroupPage = checkGroupDao.findByPage(queryPageBean.getQueryString());
        return new PageResult(checkGroupPage.getTotal(),checkGroupPage.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        log.debug(">>>>>>>>id:{}"+id);
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        log.debug(">>>>>>>>checkGroupId:{}"+checkGroupId);
        List<Integer> ids = checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
        log.debug(">>>>>>>>ids:{}"+ids);
        return ids;
    }

    @Transactional
    @Override
    public void update(Integer[] ids, CheckGroup checkGroup) {
        log.debug(">>>>>>>>checkGroup:{} ids:{}"+checkGroup,ids);
        //更新
        checkGroupDao.update(checkGroup);
        //删除关系
        checkGroupDao.deleteCheckItemIdsByCheckGroupId(checkGroup.getId());
        //新增关系
        for (Integer id : ids) {
            HashMap map = new HashMap();
            map.put("checkgroup_id",checkGroup.getId());
            map.put("checkitem_id",id);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

}
